<template>
  <div style="height: 6vh;background-color:#fff;padding:10px 20px;">
    <span style="line-height:60px;font-size:20px;">围栏管理</span>
    <el-button type="primary" style="float:right;margin-top:13px;" @click="router.push('/geofenceMap')">新建围栏</el-button>
  </div>
  <!--第6步:创建地图容器元素-->
  <div style="width:100%;height:84vh;" id="mapContainer"></div>
  <!--新增电子围栏的弹窗-->
  <el-dialog v-model="dialogVisible" title="围栏信息" :before-close="closeDialog">
    <el-form label-width="80px">
      <el-form-item label="围栏名称">
        <el-input v-model="geofence.name" placeholder="请输入围栏名称"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" @click="saveGeofence">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { DrawScene, DrawControl ,OperateEventType} from 'bmap-draw';
import router from "@/router";
import {nextTick, onMounted, ref} from "vue";
import {ElMessage} from "element-plus";
import axios from "axios";
import qs from "qs";

//定义电子围栏信息
const geofence = ref({
  name:"",//围栏名称
  position:{}//围栏信息
});
//控制新增围栏名称输入框是否显示
const dialogVisible =ref(false);
let map;
onMounted(()=>{
  //表示在DOM更新之后执行,确保地图能在div容器创建之后再渲染
  nextTick(()=>{
    //第7步:创建地图实例,放入之前准备好的容器中,注意不要导包！忽略提示
    map = new BMapGL.Map("mapContainer");
    //第8步:设置地图中心点
    let point = new BMapGL.Point(116.4074,39.9042);
    //第9步:地图初始化,同时设置地图中心点与缩放级别
    map.centerAndZoom(point,15);
    // 添加鼠标绘制控件
    //1.创建场景类，这是添加其它绘制控件的前提,DrawScene需要导入！
    const scene = new DrawScene(map);
    //2.创建控件类,并将上一步创建好的场景作为参数传入,DrawControl需要导入！
    const drawContrl = new DrawControl(scene, {
      //3.不显示对应图标用途提示
      enableTips: true,
      //4.控件位于地图的右上角
      anchor: BMAP_ANCHOR_TOP_RIGHT,
      //5.绘制项数组,包含圆形与矩形
      drawingItems: [
        'circle',
        'rectangle'
      ]
    });
    //围栏绘制结束要添加监听事件,返回当前的电子围栏元素
    scene.addEventListener(OperateEventType.COMPLETE,(event)=>{
      //绘制完成,点击"√"，显示新增围栏弹窗
      dialogVisible.value=true;
      let overlay = event.target.overlay;
      if (overlay instanceof BMapGL.Circle){
        geofence.value.position.type = 'circle';
        geofence.value.position.longtitude = overlay.getCenter().lng;
        geofence.value.position.latitude = overlay.getCenter().lat;
        geofence.value.position.radius = overlay.getRadius();
        console.log(geofence.value.position);
      }else {//绘制的是矩形
        //定义数组用来保存矩形的四个顶点坐标
        let recPoints = [];
        //依次获取矩形的四个顶点坐标并保存到数组中
        for(let i = 0; i < 4; i++){
          //overlay.points[i] 5个点 0,1,2,3循环四次,也就是依次获取四个顶点的经纬度值，存入recPoints数组中
          recPoints.push(overlay.points[i].latLng.lng+'-'+overlay.points[i].latLng.lat);
        }
        console.log(recPoints);
        //将数组中的元素用逗号分隔并保存到data对象中
        console.log(recPoints.join(','));
        //设置围栏类型为矩形
        geofence.value.position.type='rectangle';
        //将顶点坐标数组中的元素用逗号分隔并保存到position属性里
        geofence.value.position.recPoints = recPoints.join(',');
        console.log(geofence.value.position);//{type:'rectangle',recPoints:'116.38124134354936-39.91878313185032,116.38734982101722-39.91878313185032,116.38734982101722-39.91324913449542,116.38124134354936-39.91324913449542'}
      }
    });
    //6.将创建好的绘制控件添加到地图中
    map.addControl(drawContrl);
  })
});
//新增围栏
const saveGeofence =()=>{
  if (!geofence.value.name||geofence.value.name.trim().length==0){
    ElMessage.error('围栏名称不能为空!');
    return;
  }
  let data =qs.stringify(geofence.value);
  axios.post(BASE_URL+'/v1/geofence/save?'+data).then(
      (res)=>{
        if (res.data.code == 2000){
          ElMessage.success('添加成功!');
          dialogVisible.value=false;
          //跳至电子围栏管理页
          router.push('/geofence');
        }else{
          ElMessage.error(res.data.msg);
        }
      }
  )
};
//定义关闭弹窗的方法
const closeDialog=()=>{
  if(confirm('确认取消新增围栏吗?')){
    dialogVisible.value = false;
    map.clearOverlays();
  }
}
</script>


<style scoped>

</style>